package com.jzy.bxb.app.scripts.http.user;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jzy.bxb.app.manager.RedisManager;
import com.jzy.bxb.core.handler.HandlerEntity;
import com.jzy.bxb.core.handler.HttpHandler;
import com.jzy.bxb.core.util.MsgUtil;
import com.jzy.bxb.core.util.StringUtil;
import com.jzy.bxb.model.constant.AppConstant;
import com.jzy.bxb.model.constant.RedisKey;
import com.jzy.bxb.model.util.SMSUtil;

import redis.clients.jedis.Jedis;

/**
 * 发送验证码 TODO 重发验证
 * <p>
 *  http://127.0.0.1:8680/user/verification.code?userName=18782024395<br>
 *  http://112.74.36.233:8680/user/verification.code?userName=18782024395<br>
 * </p>
 * @author JiangZhiYong QQ:359135103
 */
@HandlerEntity(path = "/user/verification.code")
public class SendVerificationCodeHandler extends HttpHandler {
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginHandler.class);

	@Override
	public void run() {
		String userName = getString("userName");
		if (userName == null || !StringUtil.isMobile(userName)) {
			sendError("请输入手机号");
		}
		int code = SMSUtil.sendVerificationCode("帮主,", userName);
		Jedis client = RedisManager.getInstance().getClient();
		client.setex(RedisKey.UserVerificationCode.getKey(userName), AppConstant.VERIFICATION_CODE_TIME*60,
				String.valueOf(code));
		client.close();
		LOGGER.info("用户{} {} 发送验证码{}", MsgUtil.getIp(getSession()), userName, code);
		sendMsg("验证码已发送，请注意查收");
	}

}
